﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class CSManager_UserControl_uc_LapPhieu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {
        lblAlerting.Text = string.Empty;
        txtNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        GetBuoi();
    }
    private void GetBuoi()
    {
        try
        {
            CateRouteBO cate = new CateRouteBO();
            List<ZD_PRC_ZD_CATE_BUOI_GETBYIDResult> lst = new List<ZD_PRC_ZD_CATE_BUOI_GETBYIDResult>();
            lst = cate.BuoiGet(-1);
            if (lst != null)
            {
                ddlBUOI.DataSource = lst;
                ddlBUOI.DataTextField = "TENBUOI";
                ddlBUOI.DataValueField = "BUOIID";
                ddlBUOI.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "0");
                ddlBUOI.Items.Insert(0, lstParent);
                ddlBUOI.SelectedIndex = ddlBUOI.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblAlerting.Text = string.Empty;
        DateTime dt = new DateTime();
        try
        {
            dt = DateTime.ParseExact(txtNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }
        catch
        {
            lblAlerting.Text = "Bạn chưa nhập ngày hoặc ngày không đúng!";
            return;
        }
        if (int.Parse(ddlBUOI.SelectedValue) <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn buổi!";
            return;
        }

        DeliveryBO del = new DeliveryBO();
        List<ZD_PRC_ZD_DELIVERY_SO_TEMP_CS_MAN_GETResult> result = new List<ZD_PRC_ZD_DELIVERY_SO_TEMP_CS_MAN_GETResult>();
        result = del.Delivery_SO_Temp_Detail_CS_Man_View(dt, int.Parse(ddlBUOI.SelectedValue));
        grdList.DataSource = result;
        grdList.DataBind();

        
        List<ZD_PRC_ZD_DELIVERY_SO_CS_MAN_GETBYIDResult> rs = new List<ZD_PRC_ZD_DELIVERY_SO_CS_MAN_GETBYIDResult>();
        rs = del.Delivery_SO_CS_Man_Get(dt, int.Parse(ddlBUOI.SelectedValue));

        if (rs.Count > 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (btnSave.Visible)
        {
            try
            {
                bool check = false;
                if (grdList.Rows.Count > 0)
                {
                    for (int i = 0; i < grdList.Rows.Count; i++)
                    {
                        // Cell 0 la checkbox
                        CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[0].FindControl("chkSave"));
                        if (chkSave.Checked)
                        {
                            check = true;
                            break;


                        }
                    }
                    if (!check)
                    {
                        lblAlerting.Text = "Không có dữ liệu lập phiếu xuất!";
                        return;
                    }

                }
                else
                {
                    lblAlerting.Text = "Không có dữ liệu lập phiếu xuất!";
                    return;
                }
                string strDSID = string.Empty;

                for (int i = 0; i < grdList.Rows.Count; i++)
                {
                    // Cell 0 la checkbox
                    CheckBox chkSave = ((CheckBox)grdList.Rows[i].Cells[0].FindControl("chkSave"));
                    if (chkSave.Checked)
                    {
                        string strID = ((HiddenField)grdList.Rows[i].Cells[0].FindControl("hdfID")).Value;
                        strDSID += strID + "@";


                    }
                }
                string Link = "../csview/sv_" + txtNgay.Text.Trim().Replace("/", "") + "_" + ddlBUOI.SelectedValue + "_" + strDSID;
                Response.Redirect(Link);
            }

            catch (Exception ex)
            {


            }
        }
    }

}
